package com.linkoog.devtools.archive.nodes;

import com.intellij.ide.projectView.ViewSettings;
import com.intellij.ide.projectView.impl.nodes.PsiFileNode;
import com.intellij.ide.util.treeView.AbstractTreeNode;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.impl.PsiManagerImpl;
import com.intellij.psi.impl.file.PsiDirectoryImpl;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collection;


public class PsiDirectoryNodeWrapper extends PsiFileNode {

    private final VirtualFile _archiveFile;
    private final boolean _isNestedFile;

    public PsiDirectoryNodeWrapper(Project project, @NotNull PsiFile value, ViewSettings viewSettings, boolean isNestedFile, VirtualFile archiveFile) {
        super(project, value, viewSettings);
        this._isNestedFile = isNestedFile;
        this._archiveFile = archiveFile;
    }

    @Override
    public Collection<AbstractTreeNode<?>> getChildrenImpl() {
        final PsiManager psiManager = PsiManager.getInstance(getProject());
        PsiDirectory psiDir;
        if (_isNestedFile) {
            psiDir = new PsiDirectoryImpl((PsiManagerImpl)psiManager, _archiveFile);
        } else {
            psiDir = psiManager.findDirectory(_archiveFile);
        }

        ArrayList<AbstractTreeNode<?>> list = new ArrayList<>();
        if (psiDir != null) {
            list.addAll(new ArchiveBasedPsiNode(getProject(), psiDir, _archiveFile, getSettings()).getChildren());
        }
        return list;
    }

}